package com.gxa.ssm251.controller;

import com.gxa.ssm251.exception.BusinessException;
import com.gxa.ssm251.pojo.Admin;
import com.gxa.ssm251.pojo.City;
import com.gxa.ssm251.pojo.Cp;
import com.gxa.ssm251.service.AdminService;
import com.gxa.ssm251.service.CpService;
import com.gxa.ssm251.util.JsonResult;
import com.gxa.ssm251.util.MD5Util;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.*;

import static com.alibaba.fastjson.JSONValidator.Type.Array;

//@Controller("cpController")
//@Controller
@RestController
@RequestMapping("/admin")
@Api(value = "执行管理员用户的控制器", tags = "管理员用户接口的api")
@CrossOrigin //跨域的注解
public class AdminController {

//    @Autowired
//    @Resource
//    @Qualifier("cpService")
//    private CpService cpService;

    @Resource
    private AdminService adminService;


    @ApiOperation(value = "查询全部管理员的接口", notes = "在这里不需要参数,直接调用接口",
            response = Map.class, httpMethod = "get/post")
    @PostMapping("/allAdmin")
    @ResponseBody
    public JsonResult allAdmin() {
        JsonResult jsonResult = new JsonResult();
        List<Admin> admins = adminService.selectAllAdmin();
        jsonResult.setData(admins);
        return jsonResult;
    }

    @ApiOperation(value = "添加管理员的接口")
    @ApiImplicitParams(
            @ApiImplicitParam(name = "admin", value = "json格式的管理员对象", dataType = "json")
    )
    @PostMapping("/add")
    @ResponseBody
    public JsonResult addAdmin(@RequestBody Map map) {
        Admin admin=new Admin();
        admin.setName(map.get("name").toString());
        String uuid=UUID.randomUUID().toString().
                replace("-", "").substring(0,8);
        admin.setPwd(MD5Util.encode(map.get("pwd").toString()+uuid));
        admin.setSalt(uuid);
        admin.setEmail(map.get("email").toString());
        admin.setInUse(Boolean.valueOf(map.get("inUse").toString()));
        JsonResult jsonResult = new JsonResult();
        Integer integer=0;
        Integer integer1=0;
        //调用添加方法
      try {
          integer = adminService.addAdmin(admin);
          //返回刚刚添加的Admin
          Admin loadmin= adminService.selecAdmintByName(admin.getName());
          String limits = map.get("limits").toString();
          List<String> list = new ArrayList<String>();
          String strlist[] = limits.split(",");
          list = Arrays.asList(strlist);
          integer1= adminService.addLimit(loadmin.getId(),list);
          if (integer > 0 && integer1>0) {
              jsonResult.setMsg("添加成功");
              jsonResult.setCode("1");
          }else {
              jsonResult.setMsg("未知原因添加失败");
              jsonResult.setCode("0");
          }
      }catch (Exception e){
          jsonResult.setMsg(e.getMessage());
          jsonResult.setCode("0");
      }
        return jsonResult;
    }

    @ApiOperation(value = "修改管理员可用状态的接口")
    @ApiImplicitParams(
            @ApiImplicitParam(name = "admin", value = "json格式的管理员对象", dataType = "json")
    )
    @PostMapping("/upStatus")
    @ResponseBody
    public JsonResult updateStatus(@RequestBody Admin admin) {
        JsonResult jsonResult = new JsonResult();
        Integer integer = adminService.upStatus(admin);
        if (integer > 0) {
            jsonResult.setMsg("修改成功");
            jsonResult.setCode("1");
        } else {
            jsonResult.setMsg("修改失败");
            jsonResult.setCode("0");
        }
        return jsonResult;
    }

    @ApiOperation(value = "修改管理员信息")
    @ApiImplicitParams(
            @ApiImplicitParam(name = "admin", value = "json格式的管理员对象", dataType = "json")
    )
    @ResponseBody
    @PostMapping("/upAdmin")
    public JsonResult updateAdmin(@RequestBody Admin admin) {
        JsonResult jsonResult = new JsonResult();
        Integer integer = adminService.upAdmin(admin);
        if (integer > 0) {
            jsonResult.setMsg("修改成功");
            jsonResult.setCode("1");
        } else {
            jsonResult.setMsg("修改失败");
            jsonResult.setCode("0");
        }
        return jsonResult;
    }


    @ApiOperation(value = "删除管理员的接口")
    @ApiImplicitParams(
            @ApiImplicitParam(name = "admin", value = "json格式的管理员对象", dataType = "json")
    )
    @ResponseBody
    @PostMapping("/delete")
    public JsonResult deleteAdmin(@RequestBody Admin admin) {
        JsonResult jsonResult = new JsonResult();
        Integer integer = adminService.deleteAdmin(admin.getId());
        if (integer > 0) {
            jsonResult.setMsg("删除成功");
            jsonResult.setCode("1");
        } else {
            jsonResult.setMsg("未知原因,删除失败");
            jsonResult.setCode("0");
        }
        return jsonResult;
    }


    @ApiOperation(value = "管理员用户的登录接口", notes = "实现登录")
    @ApiImplicitParams(
            {@ApiImplicitParam(name = "name", value = "管理员的用户名", required = true, dataType = "string"),
                    @ApiImplicitParam(name = "pwd", value = "管理员的用户密码", required = true, dataType = "string")
            })
    @PostMapping("/login")
    @ResponseBody
    public JsonResult login(@RequestBody Admin admin) {
        JsonResult jsonResult = new JsonResult();
        Admin login = null;
        try {
            System.out.println(admin.getName());
            login = adminService.login(admin.getName(), admin.getPwd());
            if (login != null) {
                login.setLastTime(new Date());
               //更新每次登录的时间
                Integer integer = adminService.setTime(login);
                jsonResult.setCode("1");
                jsonResult.setMsg("登录成功!");
                jsonResult.setData(login);
                jsonResult.setValue("token",UUID.randomUUID().toString());
                //调用返回权限的值
                List limits=adminService.selectAllLimits(login.getId());
                jsonResult.setValue("limit",limits);
            }
        } catch (Exception e) {
            jsonResult.setCode("0");
//            if (e instanceof BusinessException) {
                jsonResult.setMsg(e.getMessage());

//            }
        }
//        System.out.println(jsonResult.getValues().toString());
        return jsonResult;
    }
    @ApiOperation(value = "查询全部城市的接口", notes = "在这里不需要参数,直接调用接口",
            response = Map.class, httpMethod = "get/post")
    @PostMapping("/allCity")
    @ResponseBody
    public JsonResult allcity() {
        JsonResult jsonResult = new JsonResult();
        List<City> citys = adminService.selectAllCity();
        if (citys.size()>0){
            jsonResult.setCode("1");
            jsonResult.setMsg("返回成功");
            jsonResult.setData(citys);
        }else{
            jsonResult.setCode("0");
            jsonResult.setMsg("返回值失败,请检查code值");
        }
        return jsonResult;
    }

    @ApiOperation(value = "删除城市的接口")
    @ApiImplicitParams(
            @ApiImplicitParam(name = "City", value = "json格式的城市对象", dataType = "json")
    )
    @ResponseBody
    @PostMapping("/deleteCity")
    public JsonResult deleteAdmin(@RequestBody City city) {
        JsonResult jsonResult = new JsonResult();
        Integer integer = adminService.deleteCity(city.getId());
        if (integer > 0) {
            jsonResult.setMsg("删除成功");
            jsonResult.setCode("1");
        } else {
            jsonResult.setMsg("未知原因,删除失败");
            jsonResult.setCode("0");
        }
        return jsonResult;
    }

    @ApiOperation(value = "添加城市的接口")
    @ApiImplicitParams(
            @ApiImplicitParam(name = "City", value = "json格式的管理员对象", dataType = "json")
    )
    @PostMapping("/addCity")
    @ResponseBody
    public JsonResult addCity(@RequestBody City city) {
       JsonResult jsonResult =new JsonResult();
       try {
           Integer integer=adminService.addCity(city);
           if (integer>0){
               jsonResult.setCode("1");
               jsonResult.setMsg("添加成功");
           }else {
               jsonResult.setCode("0");
               jsonResult.setMsg("未知添加失败");
           }
       }catch (Exception e){
           if (e instanceof BusinessException){
               jsonResult.setCode("0");
               jsonResult.setMsg(e.getMessage());
           }
       }

        return jsonResult;
    }

}
